﻿#include "adminui.h"
#include "ui_adminui.h"

adminUI::adminUI(QSqlDatabase* db,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::adminUI)
{
    ui->setupUi(this);
    m_db = *db;//初始化数据库连接
    \
    this->setCentralWidget(ui->stackedWidget);//设置中心组件

    ini();//初始化

    ui->stackedWidget->setCurrentWidget(ui->pagSick);//设置病人界面为初始界面
}

adminUI::~adminUI()
{
    delete ui;
}

void adminUI::ini()
{
    m_sickModel = new QSqlTableModel(this);//病人数据模型
    m_doctorModel = new QSqlTableModel(this);//医生数据模型
    m_caseModel = new QSqlTableModel(this);//病例数据模型
    m_reserveModel = new QSqlTableModel(this);//预约数据模型
    m_feedbackModel = new QSqlTableModel(this);//反馈数据模型

    //绑定数据库
    m_sickModel->setTable("tb_sick");
    m_doctorModel->setTable("tb_doctor");
    m_caseModel->setTable("tb_case");
    m_reserveModel->setTable("tb_reserve");
    m_feedbackModel->setTable("tb_feedback");

    //同步数据
    m_sickModel->select();
    m_doctorModel->select();
    m_caseModel->select();
    m_reserveModel->select();
    m_feedbackModel->select();

    //设置人工提交数据
    m_sickModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_doctorModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_caseModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_reserveModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_feedbackModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

    //设置表格自动拉伸
    ui->tvSick->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvDoctor->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvCase->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvReserve->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvFeedback->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    //修改表头
//    m_sickModel->setHeaderData(0,Qt::Horizontal,"编号");
//    m_sickModel->setHeaderData(1,Qt::Horizontal,"账号");
//    m_sickModel->setHeaderData(2,Qt::Horizontal,"密码");
//    m_sickModel->setHeaderData(3,Qt::Horizontal,"年龄");
//    m_sickModel->setHeaderData(4,Qt::Horizontal,"性别");
//    m_sickModel->setHeaderData(5,Qt::Horizontal,"电话");
//    m_sickModel->setHeaderData(6,Qt::Horizontal,"住址");
//    m_sickModel->setHeaderData(7,Qt::Horizontal,"类型");

//    m_doctorModel->setHeaderData(0,Qt::Horizontal,"编号");
//    m_doctorModel->setHeaderData(1,Qt::Horizontal,"账号");
//    m_doctorModel->setHeaderData(2,Qt::Horizontal,"密码");
//    m_doctorModel->setHeaderData(3,Qt::Horizontal,"年龄");
//    m_doctorModel->setHeaderData(4,Qt::Horizontal,"性别");
//    m_doctorModel->setHeaderData(5,Qt::Horizontal,"电话");
//    m_doctorModel->setHeaderData(6,Qt::Horizontal,"住址");
//    m_doctorModel->setHeaderData(7,Qt::Horizontal,"类型");

//    m_caseModel->setHeaderData(0,Qt::Horizontal,"编号");
//    m_caseModel->setHeaderData(1,Qt::Horizontal,"病人编号");
//    m_caseModel->setHeaderData(2,Qt::Horizontal,"病人账号");
//    m_caseModel->setHeaderData(3,Qt::Horizontal,"医生编号");
//    m_caseModel->setHeaderData(4,Qt::Horizontal,"医生账号");
//    m_caseModel->setHeaderData(5,Qt::Horizontal,"描述");
//    m_caseModel->setHeaderData(6,Qt::Horizontal,"处方");

//    m_reserveModel->setHeaderData(0,Qt::Horizontal,"编号");
//    m_reserveModel->setHeaderData(1,Qt::Horizontal,"病人编号");
//    m_reserveModel->setHeaderData(2,Qt::Horizontal,"病人账号");
//    m_reserveModel->setHeaderData(3,Qt::Horizontal,"预约时间");
//    m_reserveModel->setHeaderData(4,Qt::Horizontal,"类型");

//    m_feedbackModel->setHeaderData(0,Qt::Horizontal,"编号");
//    m_feedbackModel->setHeaderData(1,Qt::Horizontal,"病人编号");
//    m_feedbackModel->setHeaderData(2,Qt::Horizontal,"病人账号");
//    m_feedbackModel->setHeaderData(3,Qt::Horizontal,"内容");

    //绑定视图和模型
    ui->tvSick->setModel(m_sickModel);
    ui->tvDoctor->setModel(m_doctorModel);
    ui->tvCase->setModel(m_caseModel);
    ui->tvReserve->setModel(m_reserveModel);
    ui->tvFeedback->setModel(m_feedbackModel);
}

void adminUI::on_btnAppendSick_clicked()
{
    m_sickModel->insertRow(m_sickModel->rowCount());
}


void adminUI::on_actSick_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagSick);
}


void adminUI::on_actDoctor_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagDoctor);
}


void adminUI::on_actCase_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagCase);
}


void adminUI::on_actReserve_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagReserve);
}


void adminUI::on_actFeedback_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagFeedback);
}


void adminUI::on_btnDeleteSick_clicked()
{
    if(ui->tvSick->currentIndex().isValid()){
        m_sickModel->removeRow(ui->tvSick->currentIndex().row());
    }
}


void adminUI::on_btnResetSick_clicked()
{
    m_sickModel->select();
}


void adminUI::on_btnSubmitSick_clicked()
{
    if(m_sickModel->isDirty()){
        if(m_sickModel->submitAll()){
            QMessageBox::information(0,"Information","Submit Successfully!");
        }else{
            QMessageBox::warning(0,"Warning","Submit failure");
        }
    }else{
        QMessageBox::warning(0,"Warning","The table has not been modified..");
    }
}


void adminUI::on_btnAppendDoctor_clicked()
{
    m_doctorModel->insertRow(m_doctorModel->rowCount());
}


void adminUI::on_btnDeleteDoctor_clicked()
{
    if(ui->tvDoctor->currentIndex().isValid()){
        m_doctorModel->removeRow(ui->tvDoctor->currentIndex().row());
    }
}


void adminUI::on_btnResetDoctor_clicked()
{
    m_doctorModel->select();
}


void adminUI::on_btnSubmitDoctor_clicked()
{
    if(m_doctorModel->isDirty()){
        if(m_doctorModel->submitAll()){
            QMessageBox::information(0,"Information","Submit successfully!");
        }else{
            QMessageBox::warning(0,"Warning","Submit failure.");
        }
    }else{
        QMessageBox::warning(0,"Warning","The table has not been modified.");
    }
}


void adminUI::on_btnAppendCase_clicked()
{
    m_caseModel->insertRow(m_caseModel->rowCount());
}


void adminUI::on_btnDeleteCase_clicked()
{
    if(ui->tvCase->currentIndex().isValid()){
        m_caseModel->removeRow(ui->tvCase->currentIndex().row());
    }
}


void adminUI::on_btnResetCase_clicked()
{
    m_caseModel->select();
}


void adminUI::on_btnSubmitCase_clicked()
{
    if(m_caseModel->isDirty()){
        if(m_caseModel->submitAll()){
            QMessageBox::information(0,"Information","Submit successfully!");
        }else{
            QMessageBox::warning(0,"Warning","Submit failure.");
        }
    }else{
        QMessageBox::warning(0,"Warning","The table has not been modified.");
    }
}


void adminUI::on_btnAppendReserve_clicked()
{
    m_reserveModel->insertRow(m_reserveModel->rowCount());
}


void adminUI::on_btnDeleteReserve_clicked()
{
    if(ui->tvReserve->currentIndex().isValid()){
        m_reserveModel->removeRow(ui->tvReserve->currentIndex().row());
    }
}


void adminUI::on_btnResetReserve_clicked()
{
    m_reserveModel->select();
}


void adminUI::on_btnSubmitReserve_clicked()
{
    if(m_reserveModel->isDirty()){
        if(m_reserveModel->submitAll()){
            QMessageBox::information(0,"Information","Submit successfully!");
        }else{
            QMessageBox::warning(0,"Warning","Submit failure.");
        }
    }else{
        QMessageBox::warning(0,"Warning","The table has not been modified.");
    }
}


void adminUI::on_btnAppendFeedback_clicked()
{
    m_feedbackModel->insertRow(m_feedbackModel->rowCount());
}


void adminUI::on_btnDeleteFeedback_clicked()
{
    if(ui->tvFeedback->currentIndex().isValid()){
        m_feedbackModel->removeRow(ui->tvFeedback->currentIndex().row());
    }
}


void adminUI::on_btnResetFeedBack_clicked()
{
    m_feedbackModel->select();
}


void adminUI::on_btnSubmitFeedback_clicked()
{
    if(m_feedbackModel->isDirty()){
        if(m_feedbackModel->submitAll()){
            QMessageBox::information(0,"Information","Submit successfully!");
        }else{
            QMessageBox::warning(0,"Warning","Submit failure.");
        }
    }else{
        QMessageBox::warning(0,"Warning","The table has not been modified.");
    }
}

